<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
        <el-row>
            <el-col :span="8">
                <el-form-item label="商品名称" prop="name" style="width: 90%;">
                    <el-input
                    v-model="queryParams.name"
                    placeholder="请输入商品名称"
                    clearable
                    @keyup.enter="handleQuery"
                    />
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item label="商品分类" style="width: 90%;" prop="category">
                    <el-select v-model="queryParams.category" placeholder="请选择商品分类">
                        <el-option
                            v-for="dict in pb_goods_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['partybuilding:MallGoods:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" @handleQuery="handleQuery" @resetQuery="resetQuery"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="mallGoodList">
        <el-table-column label="商品名称" align="left" prop="name" min-width="250" show-overflow-tooltip="true"/>
        <el-table-column label="分类" align="left" prop="category" width="250">
          <template #default="scope">
            <dict-tag :options="pb_goods_type" :value="scope.row.category" />
          </template>
        </el-table-column>
        <el-table-column label="积分数" align="left" prop="price" width="150"/>
        <el-table-column label="库存数" align="left" prop="quantity" width="150"/>
        <el-table-column label="状态" align="left" prop="status" width="150">
          <template #default="scope">
            {{ scope.row.status === 1? "上架" : "下架" }}
          </template>
        </el-table-column>
        <el-table-column label="排序" align="left" prop="sort" width="150"/>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="250">
          <template #default="scope">
            <el-button link type="danger" @click="handleUpdate(scope.row)" v-hasPermi="['partybuilding:MallGoods:edit']">修改</el-button>
            <el-divider direction="vertical" />
            <el-button link type="danger" @click="handleUpOrDown(scope.row, 1)" v-if="scope.row.status !== 1" v-hasPermi="['partybuilding:MallGoods:edit']">上架</el-button>
            <el-divider direction="vertical" v-if="scope.row.status !== 1"/>
            <el-button link type="danger" @click="handleUpOrDown(scope.row, 0)" v-if="scope.row.status === 1" v-hasPermi="['partybuilding:MallGoods:edit']">下架</el-button>
            <el-divider direction="vertical" />
            <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['partybuilding:MallGoods:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改商品对话框 -->
      <el-dialog :title="title" v-model="open" width="50%" append-to-body style="margin-top: 9px!important;">
        <el-form ref="goodsRef" :model="form" :rules="rules" label-width="99px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="商品名称:" prop="name" style="width: 100%;">
                <el-input v-model="form.name" placeholder="请输入商品名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="商品分类:" prop="category" style="width: 100%;">
                    <el-select v-model="form.category" placeholder="请选择商品分类">
                        <el-option
                            v-for="dict in pb_goods_type"
                            :key="dict.value"
                            :label="dict.label"
                            :value="dict.value"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所需积分数:" prop="price">
                <el-input-number v-model="form.price" placeholder="请输入所需积分数" style="width: 100%;" :min="0" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="库存:" prop="quantity">
                <el-input-number v-model="form.quantity" placeholder="请输入库存" style="width: 100%;" :min="0" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序:" prop="sort">
                <el-input-number v-model="form.sort" placeholder="请输入排序" style="width: 100%;" :min="0" :max="999" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="商品图片:" prop="img">
                  <image-upload
                      v-model="form.img"
                      :multiple="true"
                      :limit="1"
                      :suggestSize="'530*380'"
                      :file-type="['jpg', 'png', 'jpeg']"
                      :file-size="200">
                  </image-upload>
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item label="商品详情:" style="width: 100%;">
                    <editor v-model="form.content" :height="190"/>
                </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>


      <el-dialog :title="specTitle" v-model="specOpen" width="31%" append-to-body style="position: relative; top: 210px;">
        <el-form ref="goodsRef" :model="specForm" :rules="specRules" label-width="109px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="规格:" prop="specName" style="width: 60%;">
                <el-input v-model="specForm.specName" placeholder="请输入规格"/>
              </el-form-item>
            </el-col>
            
            <el-col :span="24">
                <el-form-item label="图片:" prop="imgs">
                <image-upload
                    v-model="specForm.imgs"
                    :multiple="true"
                    :limit="5"
                    :suggestSize="'530*380'"
                    :file-type="['jpg', 'png', 'jpeg']"
                    :file-size="200">
                </image-upload>
                </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitSpecForm">确 定</el-button>
            <el-button @click="cancelSpec">取 消</el-button>
          </div>
        </template>
      </el-dialog>

    </div>
  </template>
  
  <script setup name="GoodsManage">
  import { listMallGoods, getMallGoods, delMallGoods, addMallGoods, updateMallGoods } from "@/api/mall/GoodsManage";
  const { proxy } = getCurrentInstance();
  const mallGoodList = ref([]);
  const open = ref(false);
  const specOpen = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const total = ref(0);
  const title = ref("");
  const specTitle = ref("新增规格");
  const { pb_goods_type } = proxy.useDict("pb_goods_type");
  
  const data = reactive({
    form: {},
    specForm: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10
    },
    rules: {
      name: [
        { required: true, message: "商品名称不能为空", trigger: "blur" }
      ],
      category: [
        { required: true, message: "请选择商品分类", trigger: "blur" }
      ],
      price: [
        { required: true, message: "请输入所需积分数", trigger: "blur" }
      ],
      quantity: [
        { required: true, message: "请输入库存", trigger: "blur" }
      ],
      img: [
        { required: true, message: "请上传商品图片", trigger: "blur" }
      ],
    }
  });
  
  const { queryParams, form, specForm, rules } = toRefs(data);
  
  /** 查询商品列表 */
  function getList() {
    loading.value = true;
    listMallGoods(queryParams.value).then(response => {
      mallGoodList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  // function addSpec(){
  //   specOpen.value = true;
  // }

  // function submitSpecForm() {
  //   specOpen.value = false;
  //   resetSpec();
  // }

  // function cancelSpec() {
  //   specOpen.value = false;
  //   resetSpec();
  // }

  // function resetSpec() {

  // }

  // 表单重置
  function reset() {
    form.value = {
      id: null,
      name: null,
      category: null,
      price: null,
      quantity: null,
      sort: null,
      img: null,
      content: null,
      status: null
    };
    proxy.resetForm("goodsRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    form.value.content = "<p></p>";
    open.value = true;
    title.value = "添加商品";
  }
  
  /** 上下架按钮操作 */
  function handleUpOrDown(row, status) {
    let title = "";
    let tip = "";
    if (status === 1) {
      title = "是否确认上架商品名称为'" + row.name + "'的数据项？";
      tip = "上架商品成功";
    } else {
      title = "是否确认下架商品名称为'" + row.name + "'的数据项？";
      tip = "下架商品成功";
    }
    let obj = JSON.parse(JSON.stringify(row));
    obj.status = status;
    proxy.$modal.confirm(title).then(function() {
      return updateMallGoods(obj);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess(tip);
    }).catch(() => {});
  }

  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getMallGoods(_id).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改商品";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["goodsRef"].validate(valid => {
      if (valid) {
        if (form.value.id != null) {
          updateMallGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          form.value.status = 1;
          addMallGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id;
    proxy.$modal.confirm('是否确认删除商品名称为"' + row.name + '"的数据项？').then(function() {
      return delMallGoods(_ids);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }

  getList();
  </script>
  